// 入口函数
(function(){
    getUserInfo()

    
    $('.quit').click(function(){
        //eg1
layer.confirm('is not?', {icon: 3, title:'提示'}, function(index){
    //do something
    localStorage.removeItem('token')
    location.href='../code/login.html'
    
    layer.close(index);
  });
    })
    
})();

// 写在入口函数外面为全局函数 方便子窗口调用
function getUserInfo(){
    axios({
        // method 为get可以省略不写
        url: '/my/userinfo',
        // headers: {
        //    Authorization: localStorage.getItem('token')

        // }
        }).then(res=>{
            if(res.data.status!==0){
                return layui.layer.msg(res.data.message);
                return
            }
            // 渲染页面

            renderAvatar(res.data.data)
        })



}       

function renderAvatar(data){
    // 用户名
    let name = data.nickname || data.username
    $('.welcome').html(`欢迎&nbsp;`+name)
    // 渲染头像
        // 判断头像是否为空
        if(data.user_pic==null){
            $('.userinfo img').hide()
            let pic = name[0].toUpperCase()
            $('.avator-text').text(pic).show()
            // $('.avator-text').show()
        }else{
            $('.avator-text').hide()
            // console.log($('.userinfo img'));
            // console.log(data.user_pic);
            
            $('.userinfo img').show().attr('src',data.user_pic)
        }

}